package com.dayouzi.crawler_monitor.service.impl;

import com.dayouzi.crawler_monitor.config.DataSource;
import com.dayouzi.crawler_monitor.mapper.Yeji241Mapper;
import com.dayouzi.crawler_monitor.mapper.Yeji252Mapper;
import com.dayouzi.crawler_monitor.mapper.YejiDetailMapper;
import com.dayouzi.crawler_monitor.pojo.page.PageBean;
import com.dayouzi.crawler_monitor.pojo.vo.YejiDetailVO;
import com.dayouzi.crawler_monitor.service.YejiDetailService;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Slf4j
public class YejiDetailServiceImpl implements YejiDetailService {

    @Autowired
    private YejiDetailMapper yejiDetailMapper;

    @Autowired
    private Yeji241Mapper yeji241Mapper;

    @Autowired
    private Yeji252Mapper yeji252Mapper;

    @Override
    public YejiDetailVO yejiDetail(Integer id) {
        return yejiDetailMapper.yejiDetail(id);
    }

    @Override
    @DataSource(value = "yeji241")
    public ArrayList<String> getYejiFieldList241(String tableName) {
        ArrayList<String> result = new ArrayList<>();
        String[] splits = tableName.split(">");
        String table = splits[2];
        ArrayList<Map<Object, Object>> fieldList = yeji241Mapper.getYejiFieldList241(table);
        for (Map<Object, Object> map : fieldList) {
            result.add((String) map.get("Field"));
        }
        return result;
    }

    @Override
    @DataSource(value = "yeji241")
    public PageBean<HashMap<String, String>> getYejiDataList241(Integer page, Integer limit, String tableName) {
        String[] splits = tableName.split(">");
        String table = splits[2];
        PageHelper.startPage(page, limit);
        List<HashMap<String, String>> list = yeji241Mapper.getYejiDataList241(table);
        return new PageBean<>(list);
    }

    @Override
    @DataSource(value = "suidaobig")
    public ArrayList<String> getYejiFieldList252(String tableName) {
        ArrayList<String> result = new ArrayList<>();
        String[] splits = tableName.split(">");
        String table = splits[2];
        ArrayList<Map<Object, Object>> fieldList = yeji252Mapper.getYejiFieldList252(table);
        for (Map<Object, Object> map : fieldList) {
            result.add((String) map.get("Field"));
        }
        return result;
    }

    @Override
    @DataSource(value = "suidaobig")
    public PageBean<HashMap<String, String>> getYejiDataList252(Integer page, Integer limit, String tableName) {
        String[] splits = tableName.split(">");
        String table = splits[2];
        PageHelper.startPage(page, limit);
        List<HashMap<String, String>> list = yeji252Mapper.getYejiDataList252(table);
        return new PageBean<>(list);
    }
}
